import json
import unittest

import config
from base.get_driver import Get_Driver
from page.page_login import Page_Login
from parameterized import parameterized
import utils
log = utils.Logger(config.base_dir + '/log/all.log', level='info')
def build_data():
    test_data = []
    with open(config.base_dir+"/data/login.json", encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            test_data.append((case_data.get("username")
                              , case_data.get("password")
                              , case_data.get("code")
                              , case_data.get("is_success")
                              , case_data.get("expect")))

    return test_data


class Test_Login(unittest.TestCase):
    def setUp(self):
        self.login = Page_Login(Get_Driver().get_driver())
        self.login.page_login_button()

    def tearDown(self):
        Get_Driver().quit_driver()

    @parameterized.expand(build_data())
    def test_login(self, username, pwd, code, status, result):
        self.login.page_login(username, pwd, code)
        log.logger.info(f"用户名:{username},密码:{pwd},验证码:{code}")
        # 正向用例
        if status:
            self.assertTrue(self.login.page_isNo_login_success())
            self.login.page_button_exit()

        else:
            msg = self.login.page_get_error_info()
            self.assertEqual(result, msg)
            self.login.page_close_error_win()
